package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * 2023/1/16 11:31 星期一<br/>
 * <p>
 * Alert 类是Dialog类的子类，并提供对许多预构建对话框类型的支持，这些对话框类型可以轻松地显示给用户以提示响应
 *
 * @author xuMingHai
 */
public class AlertApp extends Application {

    private final AnchorPane anchorPane = new AnchorPane();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() {
        Button noneButton = new Button("NONE");
        noneButton.setOnAction(event -> {
            Alert alert = new Alert(AlertType.NONE);
            alert.initOwner(noneButton.getScene().getWindow());
            alert.show();
        });

        Button infoButton = new Button("INFO");
        infoButton.setOnAction(event -> {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.initOwner(infoButton.getScene().getWindow());
            alert.showAndWait().ifPresent(System.out::println);
        });

        Button warnButton = new Button("WARN");
        warnButton.setOnAction(event -> new Alert(AlertType.WARNING)
                .showAndWait().ifPresent(System.out::println));

        Button errorButton = new Button("ERROR");
        errorButton.setOnAction(event -> new Alert(AlertType.ERROR)
                .showAndWait().ifPresent(System.out::println));

        Button confirmationButton = new Button("CONFIRMATION");
        confirmationButton.setOnAction(event -> new Alert(AlertType.CONFIRMATION)
                .showAndWait().ifPresent(System.out::println));

        HBox hBox = new HBox(20, noneButton,
                infoButton, warnButton, errorButton, confirmationButton);
        anchorPane.getChildren().add(hBox);
        AnchorPane.setTopAnchor(hBox, 50.0);
        AnchorPane.setLeftAnchor(hBox, 50.0);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        primaryStage.setScene(new Scene(anchorPane, 800, 600));
        primaryStage.show();
    }
}
